/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function validateOrder(){
    if(valCusID()&&valOrderItem()&&dateOfOrder()&&dueDate()&&valComapny()){
        return true;
    }else{
        return false;
    }
}

function validateCustomer(){
    if(cusName()&&cusSyrname()&&phoneNumber()&&cusEmail()){
        return true;
    }else{
        return false;
    }

}



function validatecompany(){
    if(companyName()&&companyAddress()&&companyWeb()){
        return true;
    }else{
        return false;
    }

}

function valCusID(){
     var errorname;
        var res=true;
	var x=document.getElementById("customerid").value;

	if(x==""){
	errorname="Customer ID is not entered";
	document.getElementById("cusIDCheck").innerHTML=errorname;
        res=false;
	}else if(isNaN(x)){
            errorname="Customer Id is a number";
	   document.getElementById("cusIDCheck").innerHTML=errorname;
        res=false;
        }
	else {
		document.getElementById("cusIDCheck").innerHTML=" ";

	}
     return res;
}
 function valOrderItem(){
      var errorname;
        var res=true;
	var x=document.getElementById("orderitem").value;

	if(x==""){
	errorname="Order Item is not entered";
	document.getElementById("orderItemCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("orderItemCheck").innerHTML=" ";

	}
     return res;

 }
 function dateOfOrder(){
         var errorname;
        var res=true;
	var x=document.getElementById("dateoforder").value;

	if(x==""){
	errorname="Date of Order is not entered";
	document.getElementById("dateofCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("dateofCheck").innerHTML=" ";

	}
     return res;

 }

 function dueDate(){
      var errorname;
        var res=true;
	var x=document.getElementById("duedate").value;

	if(x==""){
	errorname="Due date  is not entered";
	document.getElementById("duedateCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("duedateCheck").innerHTML=" ";

	}
     return res;


 }
function valComapny(){
     var errorname;
        var res=true;
	var x=document.getElementById("ordercompany").value;

	if(x==""){
	errorname="Order Company is not entered";
	document.getElementById("orcomCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("orcomCheck").innerHTML=" ";

	}
     return res;
}


function cusName(){
    var errorname;
        var res=true;
	var x=document.getElementById("givenname").value;

	if(x==""){
	errorname="Customer name not entered";
	document.getElementById("givennameCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("givennameCheck").innerHTML=" ";

	}
     return res;

}
function cusSyrname(){
     var errorname;
        var res=true;
	var x=document.getElementById("surname").value;

	if(x==""){
	errorname="surname name not entered";
	document.getElementById("surnameCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("surnameCheck").innerHTML=" ";

	}
     return res;
}
function phoneNumber(){
     var res=true;
    	var num=document.getElementById("phoneno").value;
	document.getElementById("phonenumberCheck").innerHTML=" ";

	if(num.length==0){
		document.getElementById("phonenumberCheck").innerHTML="you must enter phone number";
                res=false;
	}else if(isNaN(num)){
		document.getElementById("phonenumberCheck").innerHTML=" This is not a Number";
                res=false;
	}else{

		if(num.length>14){
			document.getElementById("phonenumberCheck").innerHTML="No more than 14 numbers";
                        res=false;

		}
	}
     return res;

}
function cusEmail(){
    var res=true;
    var emal=document.getElementById("email").value;
	document.getElementById("emailCheck").innerHTML=" ";
	var attime=0;
	var dottime=0;
	var check="";
	var len=emal.length;

	if(len==0){
		document.getElementById("emailCheck").innerHTML="Must enter your email address";
                res =false;
	}
	else if(len>0){

		for(var x=0; x<len;x++){
			check=emal.charAt(x);
			if(check=="@"){
			attime++;
			}
			if(check=="."){
			dottime++;
			}
		}

		if(attime==0 || attime >1 || dottime==0){
			document.getElementById("emailCheck").innerHTML="Not a valid email address";
                        res=false;
		}

		if(attime==1){
	    	var x=emal.lastIndexOf("@");
			if(x==len-1){
				document.getElementById("emailCheck").innerHTML="Not a valid email address";
                                res=false;
			}
			if(x==0){
				document.getElementById("emailCheck").innerHTML="Not a valid email address";
                                res=false;
			}
		}

		if(dottime>0){
			var y=emal.indexOf(".");
			var z=emal.lastIndexOf(".");

			if(y==0){
			document.getElementById("emailCheck").innerHTML="Not a valid email address";
                        res=false;
			}
			if(z==len-1){
				document.getElementById("emailCheck").innerHTML="Not a valid email address";
                            res= false;
			}
		}

		if(emal.lastIndexOf("@")>emal.lastIndexOf(".")){
			document.getElementById("emailCheck").innerHTML="Not a valid email address";
                        res=false;
		}

	}
        return res;
}

function companyName(){
	var errorname;
        var res=true;
	var x=document.getElementById("companyname").value;

	if(x==""){
	errorname="Company name not entered";
	document.getElementById("cnameCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("cnameCheck").innerHTML=" ";

	}
     return res;

}

function companyAddress(){
	var errorname;
        var res=true;
	var x=document.getElementById("comaddress").value;

	if(x==""){
	errorname="Company address not entered";
	document.getElementById("caddCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("caddCheck").innerHTML=" ";

	}
     return res;

}

function companyWeb(){
	var errorname;
        var res=true;
	var x=document.getElementById("webaddress").value;

	if(x==""){
	errorname="Web address not entered";
	document.getElementById("cwebCheck").innerHTML=errorname;
        res=false;
	}
	else {
		document.getElementById("cwebCheck").innerHTML=" ";

	}
     return res;

}

function updateClock ( ){

  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
